/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        MOSS Usage Tools
/// Authors:        Leonid Lyublinski (leonidly@microsoft.com)
///                 Rita Zhang (ritazh@microsoft.com)
/// Company:        Microsoft Services
/// Date:           05/04/2008  
/// Version:        1.0
///
/// ===========================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace Microsoft.SharePoint.Administration.UsageSettings
{
    class UsageLogProcessorReceiver : SPFeatureReceiver
    {

        #region Private Fields
        private const string CUSTOM_TIMER_JOB_NAME = "Usage Log Processor Timer Job";

        #endregion

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPFarm farm = properties.Feature.Parent as SPFarm;

            // make sure the job isn't already registered base on the timer service associated with the local farm
            foreach (SPJobDefinition job in SPFarm.Local.TimerService.JobDefinitions)
            {
                if (job.Name == CUSTOM_TIMER_JOB_NAME)
                {
                    job.Delete();
                }
            }

            // create and install the job
            CustomTimerJob customTimerJob = new CustomTimerJob(CUSTOM_TIMER_JOB_NAME, SPFarm.Local.TimerService);
            customTimerJob.Title = CUSTOM_TIMER_JOB_NAME;
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            customTimerJob.Schedule = schedule;
            //initially after deployment, job is disabled; needs user interaction to start it
            customTimerJob.IsDisabled = true;

            SPFarm.Local.TimerService.JobDefinitions.Add(customTimerJob);
            customTimerJob.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            foreach (SPJobDefinition job in SPFarm.Local.TimerService.JobDefinitions)
            {
                if (job.Name == CUSTOM_TIMER_JOB_NAME)
                    job.Delete();
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
           
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
